/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.power.visual;

import com.power.utils.PowerImage;
import com.power.utils.SortableArrayList;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 *
 * @author f6036477
 */
public class SortableColumnList
{

  private JPanel base;

  private List<JList> cols;

  private List<JButton> headers;

  private SortableArrayList objects;

  private static final PowerImage
      ARROW_UP = new PowerImage(
        SortableColumnList.class
        .getResource("arrowUp.png")),
      ARROW_DOWN = new PowerImage(
        SortableColumnList.class
        .getResource("arrowDown.png"));

  public static final Color LIGHT_BLUE_GREEN =
      new Color(204, 255, 255);


  public SortableColumnList()
  {
    base = new JPanel();
    base.setLayout(null);
    base.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    cols = new LinkedList<JList>();
    objects = new SortableArrayList();
    headers = new LinkedList<JButton>();
  }

  public SortableArrayList getObjects()
  {
    return objects;
  }

  public boolean addColumn(List l, Comparator c, String title)
  {
    boolean success = true;
    success = objects.add(l, c);

    if(!success) return success;

    headers.add(this.getHeader(title));
    cols.add(new JList());
    this.populate();

    return success;
  }

  public static JLabel getColoredLabel(String text)
  {
    JLabel label = new JLabel(text);
    label.setBackground(LIGHT_BLUE_GREEN);
    label.setHorizontalAlignment(SwingConstants.RIGHT);
    return label;
  }

  public static JLabel getNormalLabel(String text)
  {
    JLabel label = new JLabel(text);
    label.setHorizontalAlignment(SwingConstants.RIGHT);
    return label;
  }

  public JButton getHeader(String title)
  {
    final JButton button = new JButton(title);
    button.setHorizontalAlignment(SwingConstants.RIGHT);
    button.setHorizontalTextPosition(SwingConstants.LEADING);
    button.setIcon(ARROW_DOWN.getIcon());
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        if(button.getIcon().equals(ARROW_DOWN.getIcon())) {
          button.setIcon(ARROW_UP.getIcon());
          int i = headers.indexOf(button);
          objects.sort(i, false);
        } else {
          button.setIcon(ARROW_UP.getIcon());
          int i = headers.indexOf(button);
          objects.sort(i, true);
        }
        populate();
      }
    });
    return button;
  }

  public void populate()
  {
    boolean colored = false;
    Object[] os = null;
    for(int i = 0; i < objects.size(); i++) {
      List content = objects.get(i);
      JList jlist = cols.get(i);
      os = new Object[content.size()];

      for(int j = 0; j < content.size(); j++) {
        Object o = content.get(i);
        os[j] = (colored
            ? getColoredLabel(o.toString())
            : getNormalLabel(o.toString()));
      }//for
      jlist.setListData(os);
      System.out.println( jlist.getModel().getSize() );
      System.out.println( jlist.getWidth()+ " x "+ jlist.getHeight() );
    }//for
  }

  public Component updateUI()
  {
    int width = 0;
    int height = 0;
    int gap = 2;

    for(int i = 0; i < cols.size(); i++) {
      width += gap;
      if(cols.get(i).getHeight() > height)
        height = cols.get(i).getHeight();

      /*
      if(cols.get(i).getWidth() > headers.get(i).getWidth())
        headers.get(i).setSize(cols.get(i).getWidth(),
            headers.get(i).getHeight());
      else
        cols.get(i).setSize(headers.get(i).getWidth(),
            cols.get(i).getHeight());*/

      headers.get(i).setLocation(width, gap);
      cols.get(i).setLocation(width,
          gap*2 + headers.get(i).getHeight());
    }//for
    height += gap*3;
    width += gap;

    base.setSize(390, 365);
    base.setPreferredSize(new Dimension(width, height));
    base.removeAll();

    for(int i = 0; i < cols.size(); i++) {
      base.add(headers.get(i));
      base.add(cols.get(i));
    }

    base.repaint();
    System.out.println( "base.getComponents().length: "+
        base.getComponents().length);
    return base;
  }


  public static void main(String[] args)
  {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);
    f.setLocation(400, 300);

    SortableColumnList list = new SortableColumnList();

    List l = new ArrayList();

    l.add(6); l.add(4); l.add(7); l.add(9); l.add(1); l.add(3);
    l.add(10); l.add(8); l.add(5); l.add(2); l.add(11); l.add(12);
    l.add(17); l.add(16); l.add(14); l.add(13); l.add(18); l.add(15);

    Comparator c = new Comparator()
    {
      @Override
      public int compare(Object o1, Object o2) {
        if(o1 instanceof Integer && o2 instanceof Integer) {
          if(((Integer) o1).intValue() > (((Integer) o2).intValue()))
            return 1;
          else if(((Integer) o1).intValue() < (((Integer) o2).intValue()))
            return -1;
          else return 0;
        }//if
        return -1;
      }
    };

    list.addColumn(l, c, "Numbers");
    Component component = list.updateUI();
    component.setLocation(1, 1);
    f.add(component);
    f.setSize(400, 400);
    f.setVisible(true);

    f.remove(component);
    f.add(list.updateUI());
  }

}
